package com.drinks.yiniang.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.drinks.yiniang.R;

import java.io.File;

/**
 * Glide util
 * Created by han on 2017/11/25 0025.
 */

public class GlideUtils {

    public static void load(Context mContext, ImageView iv, String imgUrl) {
        if (imgUrl == null) {
            return;
        }
        Glide.with(mContext)
                .load(imgUrl)
                .into(iv);
    }

    public static void load(Fragment fragment, ImageView iv, String imgUrl) {
        if (imgUrl == null) {
            return;
        }
        Glide.with(fragment)
                .load(imgUrl)
                .into(iv);
    }

    public static void loadARGB8888(Context mContext, ImageView iv, String imgUrl) {
        if (imgUrl == null) {
            return;
        }
        Glide.with(mContext)
                .load(imgUrl)
                .apply(new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888))
                .into(iv);
    }

    public static void loadUserAvatar(Context mContext, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(mContext, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadUserAvatar(Fragment fragment, ImageView iv, String imgUrl) {
        if (imgUrl == null) {
            return;
        }
        loadWithPlaceholderAndError(fragment, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadSquareImage(Context mContext, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(mContext, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadSquareImage(Fragment fragment, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(fragment, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadBannerImage(Context mContext, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(mContext, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_goods_banner);
    }

    public static void loadBannerImage(Fragment fragment, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(fragment, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_goods_banner);
    }

    public static void loadAvatar(Context mContext, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(mContext, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadAvatar(Fragment fragment, ImageView iv, String imgUrl) {
        if (imgUrl == null) {
            return;
        }
        loadWithPlaceholderAndError(fragment, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadCircle(Context mContext, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(mContext, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadCircle(Fragment fragment, ImageView iv, String imgUrl) {
        if (imgUrl == null) {
            return;
        }
        loadWithPlaceholderAndError(fragment, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadGoodsList(Context mContext, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(mContext, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadGoodsList(Fragment fragment, ImageView iv, String imgUrl) {
        if (imgUrl == null) {
            return;
        }
        loadWithPlaceholderAndError(fragment, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    public static void loadGoodsAdList(Context mContext, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(mContext, iv, imgUrl, R.mipmap.bkg_placeholder_goods_banner, R.mipmap.bkg_placeholder_goods_banner);
    }

    public static void loadGoodsAdList(Fragment fragment, ImageView iv, String imgUrl) {
        if (imgUrl == null) {
            return;
        }
        loadWithPlaceholderAndError(fragment, iv, imgUrl, R.mipmap.bkg_placeholder_goods_banner, R.mipmap.bkg_placeholder_goods_banner);
    }

    public static void loadExchangeAdList(Context mContext, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(mContext, iv, imgUrl, R.mipmap.bkg_placeholder_goods_banner, R.mipmap.bkg_placeholder_goods_banner);
    }

    public static void loaShopList(Context mContext, ImageView iv, String imgUrl) {
        loadWithPlaceholderAndError(mContext, iv, imgUrl, R.mipmap.bkg_placeholder_square, R.mipmap.bkg_placeholder_square);
    }

    //加载本地图片
    public static void loadLocalImage(Context mContext, ImageView iv, String localPath) {
        if (localPath == null) {
            return;
        }
        File file = new File(localPath);

        Glide.with(mContext)
                .load(file)
                .into(iv);
    }

    public static void loadWithPlaceholderAndError(Context mContext, ImageView iv, String imgUrl, int placeholderId, int errorId) {
        if (placeholderId <= 0 || errorId <= 0) {
            return;
        }

        //居中图片，好多的图片形式都是这么设置的
        RequestOptions options = RequestOptions.centerCropTransform()
                .placeholder(placeholderId)
                .error(errorId);

        Glide.with(mContext)
                .load(imgUrl)
                .apply(options)
                .into(iv);
    }

    public static void loadWithPlaceholderAndError(Fragment fragment, ImageView iv, String imgUrl, int placeholderId, int errorId) {
        if (placeholderId <= 0 || errorId <= 0) {
            return;
        }

        //居中图片，好多的图片形式都是这么设置的
        RequestOptions options = RequestOptions.centerCropTransform()
                .placeholder(placeholderId)
                .error(errorId);

        Glide.with(fragment)
                .load(imgUrl)
                .apply(options)
                .into(iv);
    }

    public static void loadWithListener(Context mContext, ImageView iv, String imgUrl, RequestListener<Drawable> mListener) {
        if (imgUrl == null) {
            return;
        }

        //RequestOptions options = new RequestOptions().placeholder(R.mipmap.bg_placeholder1).error(R.mipmap.bg_placeholder1);

        Glide.with(mContext)
                .load(imgUrl)
                .listener(mListener)
                .into(iv);
    }

    public static void loadRound(Context mContext, ImageView iv, int resId, int radius) {
        Glide.with(mContext)
                .load(resId)
                //.bitmapTransform(new GrayscaleTransformation(this))//带灰色蒙层
                //.bitmapTransform(new RoundCornersTransformation(mContext, radius, RoundCornersTransformation.CornerType.TOP))
                .into(iv);
    }
}
